package com.springboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @author zz
 * @version 1.0
 * @Description
 */

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册WebSocket端点，客户端将使用它连接到WebSocket服务器
        // withSockJS() 表示启用SockJS fallback机制，用于不支持原生WebSocket的浏览器
        registry.addEndpoint("/chat").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 定义客户端发送消息的路径前缀，即应用的目的地前缀
        registry.setApplicationDestinationPrefixes("/app");
        // 定义消息代理将向所有连接并订阅特定主题的客户端广播信息的前缀
        registry.enableSimpleBroker("/topic");
    }
}